package com.ihrm.common.controller;

import io.jsonwebtoken.Claims;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author VectorX
 * @version V1.0
 * @description
 * @date 2024-05-31 11:03:58
 */
public class BaseController
{
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected String companyId;
    protected String companyName;
    protected String userId;
    protected Claims claims;

    @ModelAttribute
    public void setReqAndResp(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;

         this.companyId = "1";
         this.companyName = "传智播客";

        // 使用 shiro 从 Redis 获取数据
//        final Subject subject = SecurityUtils.getSubject();
//        final PrincipalCollection principals = subject.getPrincipals();
//        if (principals != null && !principals.isEmpty()) {
//            final ProfileResult profileResult = (ProfileResult) principals.getPrimaryPrincipal();
//            this.companyId = profileResult.getCompanyId();
//            this.companyName = profileResult.getCompany();
//        }
        // 使用jwt的方式
//        Object obj = request.getAttribute("user_claims");
//        if (obj != null) {
//            this.claims = (Claims)obj;
//            this.companyName = (String) claims.get("CompanyName");
//            this.companyId = (String) claims.get("CompanyId");
//            this.userId = claims.getId();
//        }
    }
}
